package com.sttk.fastMark.camera.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.sttk.fastMark.camera.BaseAc;
import com.sttk.fastMark.camera.Content;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.basic.utils.FileP2pUtil;
import com.sttk.fastMark.camera.basic.utils.FileUtil;
import com.sttk.fastMark.camera.databinding.ActivityPicFormatBinding;
import com.sttk.fastMark.camera.dialog.FblDialog;
import com.sttk.fastMark.camera.dialog.FormatDialog;
import com.sttk.fastMark.camera.dialog.RenameDialog;

import java.text.SimpleDateFormat;

import io.microshow.rxffmpeg.RxFFmpegInvoke;
import io.microshow.rxffmpeg.RxFFmpegSubscriber;

public class PicFormatActivity extends BaseAc<ActivityPicFormatBinding> implements View.OnClickListener {

    public static String imgPath;
    private String selFormat = "jpg";
    private String selFbl = "1080X960";
    private String newName;
    private FormatDialog formatDialog;
    private FblDialog fblDialog;
    private RenameDialog renameDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_format;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicFormatBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llFormat.setOnClickListener(this);
        mDataBinding.llFbl.setOnClickListener(this);
        mDataBinding.llName.setOnClickListener(this);
        mDataBinding.tvConvert.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llFormat) {//格式
            showFormatDialog();
        } else if (id == R.id.llFbl) {//分辨率
            showFblDialog();
        } else if (id == R.id.llName) {//名称
            showRenameDialog();
        } else if (id == R.id.tvConvert) {//立即转换
            convertImage();

        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(imgPath).into(mDataBinding.ivImg);
        newName = TimeUtils.getNowString(new SimpleDateFormat("yyyyMMdd_HHmmss"));
        mDataBinding.tvFormat.setText(selFormat);
        mDataBinding.tvFbl.setText(selFbl);
        mDataBinding.tvName.setText(newName);
    }

    private void showFormatDialog() {
        if (formatDialog == null) formatDialog = new FormatDialog(mContext);
        formatDialog.setListener(new FormatDialog.Ilistener() {
            @Override
            public void clickConfirm(String format) {
                selFormat = format;
                mDataBinding.tvFormat.setText(selFormat);
            }
        });
        formatDialog.show();
    }

    private void showFblDialog() {
        if (fblDialog == null) fblDialog = new FblDialog(mContext);
        fblDialog.setListener(new FblDialog.Ilistener() {
            @Override
            public void clickConfirm(String fbl) {
                selFbl = fbl;
                mDataBinding.tvFbl.setText(selFbl);
            }
        });
        fblDialog.show();
    }

    private void showRenameDialog() {
        if (renameDialog == null) {
            renameDialog = new RenameDialog(mContext);
            renameDialog.setText(newName);
        }
        renameDialog.setListener(new RenameDialog.Ilistener() {
            @Override
            public void clickConfirm(String text) {
                newName = text;
                mDataBinding.tvName.setText(newName);
            }
        });
        renameDialog.show();
    }

    private void convertImage() {
        if (imgPath == null || imgPath.isEmpty()) {
            ToastUtils.showShort("请先选择图片");
            return;
        }

        // 生成输出文件路径
        String outputPath = FileUtil.generateFilePathByName(Content.Temp, newName + "." + selFormat);

        // 解析分辨率
        String[] resolution = selFbl.split("X");
        if (resolution.length != 2) {
            ToastUtils.showShort("分辨率格式错误");
            return;
        }

        String width = resolution[0];
        String height = resolution[1];

        // 构建ffmpeg命令
        // -y: 覆盖输出文件
        // -i: 输入文件
        // -vf scale: 视频过滤器，调整分辨率
        // 输出文件路径
        String strCommand = "ffmpeg -y -i " + imgPath + " -vf scale=" + width + ":" + height + " " + outputPath;
        String[] commands = strCommand.split(" ");

        // 执行FFmpeg命令
        RxFFmpegInvoke.getInstance()
                .runCommandRxJava(commands)
                .subscribe(new RxFFmpegSubscriber() {
                    @Override
                    public void onFinish() {
                        dismissDialog();
                        FileP2pUtil.copyPrivateImgToPublic(mContext, outputPath);
                        ToastUtils.showShort("图片转换完成");
                        // 转换完成后直接返回到SettingActivity
                        Intent intent = new Intent(mContext, SettingActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onProgress(int progress, long progressTime) {
                        // 可以显示进度条
                        showDialog(getString(R.string.loading));
                    }

                    @Override
                    public void onCancel() {
                        dismissDialog();
                        ToastUtils.showShort("图片转换已取消");
                    }

                    @Override
                    public void onError(String message) {
                        dismissDialog();
                        ToastUtils.showShort("图片转换失败: " + message);
                    }
                });
    }
}